package com.iwomy.envpro.platform.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.iwomy.envpro.admin.api.entity.SysTenant;
import org.springframework.cglib.core.Local;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class DataLimitUtil {


	public static long convertMilliseconds(LocalDateTime dateTime, SysTenant sysTenant) throws Exception {
		Integer dataPeriod = sysTenant.getDataPeriod();
		//最大可查询时间
		LocalDateTime maxTime = LocalDateTime.now().plusDays(-dataPeriod);
		if (dateTime.compareTo(maxTime) < 0) {
			throw new Exception("查询时间超过" + dataPeriod + "天，请修改查询条件或升级版本");
		}
		return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}

	public static long convert(LocalDateTime dateTime)   {
		return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}
	public static long convert(LocalDate date)   {
		return date.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}



	public static long convertMilliseconds(LocalDate date, SysTenant sysTenant) throws Exception {
		Integer dataPeriod = sysTenant.getDataPeriod();
		//最大可查询时间  -1因为按天查询查询到前一天23：59：59
		LocalDate maxTime = LocalDate.now().plusDays(-dataPeriod - 1);
		if (date.compareTo(maxTime) < 0) {
			throw new Exception("查询时间超过" + dataPeriod + "天，请修改查询条件或升级版本");
		}
		return date.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}

	public static LocalDate judging(LocalDate date, SysTenant sysTenant) throws Exception {
		Integer dataPeriod = sysTenant.getDataPeriod();
		//最大可查询时间
		LocalDate maxTime = LocalDate.now().plusDays(-dataPeriod);
		if (date.compareTo(maxTime) < 0) {
			throw new Exception("查询时间超过" + dataPeriod + "天，请修改查询条件或升级版本");
		}
		return date;
	}

	public static LocalDateTime judging(LocalDateTime dateTime, SysTenant sysTenant) throws Exception {
		Integer dataPeriod = sysTenant.getDataPeriod();
		//最大可查询时间
		LocalDateTime maxTime = LocalDateTime.now().plusDays(-dataPeriod);
		if (dateTime.compareTo(maxTime) < 0) {
			throw new Exception("查询时间超过" + dataPeriod + "天，请修改查询条件或升级版本");
		}
		return dateTime;
	}


}
